package tcp;/*

 */

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

/**
 * @ClassName MyClientHandler
 * @Author Final
 * @Project DataStructures
 */
public class MyClientHandler extends SimpleChannelInboundHandler<ByteBuf> {

    private int count;
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {

        ByteBuf byte1=(ByteBuf) msg;
        byte[] bytes = new byte[byte1.readableBytes()];
        byte1.readBytes(bytes);

        String string = new String(bytes,CharsetUtil.UTF_8);

        System.out.println(string);
        System.out.println(++this.count);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {



    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        for (int i=0;i<10;i++){
            ByteBuf buf = Unpooled.copiedBuffer("hello，server" + i, CharsetUtil.UTF_8);

            ctx.writeAndFlush(buf);
        }

    }
}
